import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreStatService extends BaseService<any> {
  private readonly logger = new Logger(CoreStatService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getStatField
   * 对应 PHP: CoreStatService_core::getStatField()
   * 逻辑类型: undefined - undefined
   */
  async getStatField() {
    // 基于PHP真实逻辑: getStatField
    // PHP原文: return event('StatField');     }      /**      * 添加统计      * @param $param = ['addon' =>, 'date' => ['year' => , 'month'=> , 'day' => , 'hour' => ], '...
return event('StatField'];
    }

    /**
     * 添加统计
     * @param param = [addon:, date: [year: , month: , day: , hour: ], data: [shop_order_money: 12, member: 12])
     */
    async add(int site_id, param)
    {
        const model = this.statHourService;
        const condition = [
            ['site_id', '=', site_id],
            ['year', '=', param.date.year],
            ['month', '=', param.date.month],
            ['day', '=', param.date.day],
            ['addon', '=', param.addon],
        ];
        const list = model.where(condition).field('*').select().toArray(];
        if(!!list)) {
            const list = array_column(list, null, 'field');
  }

  /**
   * add
   * 对应 PHP: CoreStatService_core::add()
   * 逻辑类型: undefined - undefined
   */
  async add(site_id: any[], param: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $model = new StatHour();         $condition = [             ['site_id', '=', $site_id],             ['year', '=', $param['date']['year']],            ...
const model = this.statHourService;
        const condition = [
            ['site_id', '=', site_id],
            ['year', '=', param.date.year],
            ['month', '=', param.date.month],
            ['day', '=', param.date.day],
            ['addon', '=', param.addon],
        ];
        const list = model.where(condition).field('*').select().toArray(];
        if(!!list)) {
            const list = array_column(list, null, 'field');
  }

  /**
   * getLastTime
   * 对应 PHP: CoreStatService_core::getLastTime()
   * 逻辑类型: undefined - undefined
   */
  async getLastTime() {
    // 基于PHP真实逻辑: getLastTime
    // PHP原文: //存取缓存，如果没有缓存，会执行stat_hour,当天数据，最大时间     }      /**      * 获取日统计数据      * @param $site_id      * @param $year      * @param $month      * @param $day ...
//存取缓存，如果没有缓存，会执行stat_hour,当天数据，最大时间
    }

    /**
     * 获取日统计数据
     * @param site_id
     * @param year
     * @param month
     * @param day
     * @param fields
     */
    async getDayStat(site_id, year, month, day, fields)
    {
        const model = this.statHourService;
        const condition = [
            ['site_id', '=', site_id],
            ['year', '=', year],
            ['month', '=', month],
            ['day', '=', day],
            ['field', 'in', fields],
        ];
        const list = model.where(condition).field("*").select().toArray(];
        if(!!list))
        {
            const list = array_column(list, null, 'field');
            foreach (fields as v)
            {
                if(!array_key_exists(v, list))
                {
                    list[v] = this.statInit([site_id: site_id, year: year, month: month, day: day, field: v);
  }
  }

  /**
   * getDayStat
   * 对应 PHP: CoreStatService_core::getDayStat()
   * 逻辑类型: undefined - undefined
   */
  async getDayStat(site_id: any, year: any, month: any, day: any, fields: any) {
    // 基于PHP真实逻辑: getDayStat
    // PHP原文: $model = new StatHour();         $condition = [             ['site_id', '=', $site_id],             ['year', '=', $year],             ['month', '=', $...
const model = this.statHourService;
        const condition = [
            ['site_id', '=', site_id],
            ['year', '=', year],
            ['month', '=', month],
            ['day', '=', day],
            ['field', 'in', fields],
        ];
        const list = model.where(condition).field("*").select().toArray(];
        if(!!list))
        {
            const list = array_column(list, null, 'field');
            foreach (fields as v)
            {
                if(!array_key_exists(v, list))
                {
                    list[v] = this.statInit([site_id: site_id, year: year, month: month, day: day, field: v);
  }
  }

  /**
   * getTimePeriodStat
   * 对应 PHP: CoreStatService_core::getTimePeriodStat()
   * 逻辑类型: undefined - undefined
   */
  async getTimePeriodStat(site_id: any, fields: any, start_time: any, end_time: any) {
    // 基于PHP真实逻辑: getTimePeriodStat
    // PHP原文: $condition = [             ['site_id', '=', $site_id],             ['field', 'in', $fields],         ];         if(!empty($start_time))         {     ...
const condition = [
            ['site_id', '=', site_id],
            ['field', 'in', fields],
        ];
        if(!!start_time))
        {
            condition[] = ['start_time', '>=', start_time];
  }

  /**
   * getDayStatList
   * 对应 PHP: CoreStatService_core::getDayStatList()
   * 逻辑类型: undefined - undefined
   */
  async getDayStatList(site_id: any, field: any, start_time: any, end_time: any) {
    // 基于PHP真实逻辑: getDayStatList
    // PHP原文: $condition = [             ['site_id', '=', $site_id],             ['field', 'i=', $field],         ];         if(!empty($start_time))         {      ...
const condition = [
            ['site_id', '=', site_id],
            ['field', 'i=', field],
        ];
        if(!!start_time))
        {
            condition[] = ['start_time', '>=', start_time];
  }

  /**
   * statInit
   * 对应 PHP: CoreStatService_core::statInit()
   * 逻辑类型: undefined - undefined
   */
  async statInit(data: any) {
    // 基于PHP真实逻辑: statInit
    // PHP原文: return [             'site_id' => $data['site_id'],             'addon' => $data['addon'] ?? '',             'field' => $data['field'],             'f...
return [
            site_id: data.site_id,
            addon: data.addon || '',
            field: data.field,
            field_total: 0,
            last_time: 0,
            hour_0: 0,
            hour_1: 0,
            hour_2: 0,
            hour_3: 0,
            hour_4: 0,
            hour_5: 0,
            hour_6: 0,
            hour_7: 0,
            hour_8: 0,
            hour_9: 0,
            hour_10: 0,
            hour_11: 0,
            hour_12: 0,
            hour_13: 0,
            hour_14: 0,
            hour_15: 0,
            hour_16: 0,
            hour_17: 0,
            hour_18: 0,
            hour_19: 0,
            hour_20: 0,
            hour_21: 0,
            hour_22: 0,
            hour_23: 0,
        ;
  }
  }
}
